<template>
  <div class="second-hand-user-management">
    <!-- 操作栏 -->
    <div class="operation-bar">
      <el-input
        v-model="searchText"
        placeholder="搜索用户"
        style="width: 300px"
        clearable
      >
        <template #prefix>
          <el-icon><Search /></el-icon>
        </template>
      </el-input>
      <el-button type="primary" @click="handleAdd">
        <el-icon><Plus /></el-icon>添加用户
      </el-button>
    </div>

    <!-- 数据表格 -->
    <el-table :data="filteredUsers" border style="width: 100%">
      <el-table-column prop="userId" label="用户ID" width="120" sortable />
      <el-table-column prop="name" label="姓名" width="150" />
      <el-table-column prop="gender" label="性别" width="80">
        <template #default="{ row }">
          {{ genderOptions.find(g => g.value === row.gender)?.label }}
        </template>
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="80" sortable />
      <el-table-column prop="phone" label="联系电话" width="150" />
      <el-table-column prop="address" label="联系地址" width="200" />
      <el-table-column prop="registrationDate" label="注册日期" width="120" sortable />
      <el-table-column prop="status" label="状态" width="100">
        <template #default="{ row }">
          {{ statusOptions.find(s => s.value === row.status)?.label }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template #default="{ row }">
          <el-button size="small" @click="handleEdit(row)">编辑</el-button>
          <el-button
            size="small"
            type="danger"
            @click="handleDelete(row.userId)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加/编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="isEditMode ? '编辑用户信息' : '添加用户信息'"
      width="600px"
    >
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="80px"
      >
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="formData.userId" :disabled="isEditMode" />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-select v-model="formData.gender" placeholder="请选择">
            <el-option
              v-for="item in genderOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input-number
            v-model="formData.age"
            :min="0"
            :max="120"
            controls-position="right"
          />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="formData.phone" />
        </el-form-item>
        <el-form-item label="联系地址" prop="address">
          <el-input v-model="formData.address" />
        </el-form-item>
        <el-form-item label="注册日期" prop="registrationDate">
          <el-date-picker
            v-model="formData.registrationDate"
            type="date"
            placeholder="选择日期"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="formData.status" placeholder="请选择">
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, computed, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 性别选项
const genderOptions = [
  { value: 'male', label: '男' },
  { value: 'female', label: '女' }
]

// 状态选项
const statusOptions = [
  { value: 'active', label: '活跃' },
  { value: 'inactive', label: '非活跃' },
  { value: 'blocked', label: '封禁' }
]

// 10条完整初始用户数据
const users = ref([
  {
    userId: 'U001', name: '张三', gender: 'male', age: 25,
    phone: '13800138000', address: 'XX市XX区XX路1号',
    registrationDate: '2023-01-01', status: 'active'
  },
  {
    userId: 'U002', name: '李四', gender: 'female', age: 30,
    phone: '13800138001', address: 'XX市XX区XX路2号',
    registrationDate: '2023-02-01', status: 'inactive'
  },
  {
    userId: 'U003', name: '王五', gender: 'male', age: 22,
    phone: '13800138002', address: 'XX市XX区XX路3号',
    registrationDate: '2023-03-01', status: 'active'
  },
  {
    userId: 'U004', name: '赵六', gender: 'female', age: 35,
    phone: '13800138003', address: 'XX市XX区XX路4号',
    registrationDate: '2023-04-01', status: 'blocked'
  },
  {
    userId: 'U005', name: '孙七', gender: 'male', age: 28,
    phone: '13800138004', address: 'XX市XX区XX路5号',
    registrationDate: '2023-05-01', status: 'active'
  },
  {
    userId: 'U006', name: '周八', gender: 'female', age: 40,
    phone: '13800138005', address: 'XX市XX区XX路6号',
    registrationDate: '2023-06-01', status: 'inactive'
  },
  {
    userId: 'U007', name: '吴九', gender: 'male', age: 26,
    phone: '13800138006', address: 'XX市XX区XX路7号',
    registrationDate: '2023-07-01', status: 'active'
  },
  {
    userId: 'U008', name: '郑十', gender: 'female', age: 32,
    phone: '13800138007', address: 'XX市XX区XX路8号',
    registrationDate: '2023-08-01', status: 'blocked'
  },
  {
    userId: 'U009', name: '王十一', gender: 'male', age: 24,
    phone: '13800138008', address: 'XX市XX区XX路9号',
    registrationDate: '2023-09-01', status: 'active'
  },
  {
    userId: 'U010', name: '李十二', gender: 'female', age: 38,
    phone: '13800138009', address: 'XX市XX区XX路10号',
    registrationDate: '2023-10-01', status: 'inactive'
  }
])

// 搜索功能（包含所有字段）
const searchText = ref('')
const filteredUsers = computed(() => {
  return users.value.filter(user => {
    return Object.values(user).some(value => 
      String(value).toLowerCase().includes(searchText.value.toLowerCase())
    )
  })
})

// 对话框相关状态
const dialogVisible = ref(false)
const isEditMode = ref(false)
const formRef = ref(null)
const formData = reactive({
  userId: '',
  name: '',
  gender: 'male',
  age: 18,
  phone: '',
  address: '',
  registrationDate: '',
  status: 'active'
})

// 表单验证规则（包含所有字段）
const formRules = {
  userId: [{ required: true, message: '请输入用户ID', trigger: 'blur' }],
  name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  gender: [{ required: true, message: '请选择性别', trigger: 'change' }],
  age: [{ required: true, message: '请输入年龄', trigger: 'blur' }],
  phone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
  address: [{ required: true, message: '请输入联系地址', trigger: 'blur' }],
  registrationDate: [{ required: true, message: '请选择注册日期', trigger: 'change' }],
  status: [{ required: true, message: '请选择状态', trigger: 'change' }]
}

// 操作函数（保持原有逻辑）
const handleAdd = () => {
  isEditMode.value = false
  resetForm()
  dialogVisible.value = true
}

const handleEdit = (row) => {
  isEditMode.value = true
  Object.assign(formData, { ...row })
  dialogVisible.value = true
}

const handleDelete = (userId) => {
  ElMessageBox.confirm('确定要删除该用户吗？', '警告', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    users.value = users.value.filter(u => u.userId !== userId)
    ElMessage.success('删除成功')
  })
}

const submitForm = async () => {
  await formRef.value.validate()
  if (isEditMode.value) {
    const index = users.value.findIndex(u => u.userId === formData.userId)
    users.value.splice(index, 1, { ...formData })
  } else {
    users.value.push({ ...formData })
  }
  dialogVisible.value = false
  ElMessage.success(isEditMode.value ? '修改成功' : '添加成功')
}

const resetForm = () => {
  formData.userId = ''
  formData.name = ''
  formData.gender = 'male'
  formData.age = 18
  formData.phone = ''
  formData.address = ''
  formData.registrationDate = ''
  formData.status = 'active'
}
</script>

<style scoped>
.second-hand-user-management {
  padding: 20px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.operation-bar {
  margin-bottom: 20px;
  display: flex;
  gap: 15px;
}

.el-table {
  margin-top: 20px;
}
</style>
    